const jwt = require('jsonwebtoken')
const {PRIVATE_KEY,PUBLIC_KEY} = require('@common/config')
const {send} = require("./res")

//签发token
const webToken = (payload) => {
  const token = jwt.sign(payload,PRIVATE_KEY,{
   expiresIn: 24 * 60 * 60,//过期时间 秒
    algorithm: "RS256"    //加密算法
  })
  return token
}

//验证token
const verifyWebToken =  (token) => {
  try {
    const result = jwt.verify(token,PUBLIC_KEY,{
      algorithm: "RS256"    //加密算法
    })
    return result
  } catch (err) {
    throw new Error(err)
  }
  
}

module.exports = {
  webToken,
  verifyWebToken
}
